home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 September / macformat-004.iso / Shareware City / Graphics / VideoToolbox ƒ / Utilities / MeasureMTF / MeasureMTF.c next >
Encoding:
C/C++ Source or Header  |  1994-07-07  |  11.1 KB  |  356 lines  |  [TEXT/KAHL]

  1. /*
  2. MeasureMTF.c
  3. Copyright © 1990-1993 Denis G. Pelli
  4. Measures the modulation transfer function of a monitor. Uses drifting gratings,
  5. both horizontal and vertical, of constant temporal frequency. The results
  6. are saved in a KaleidaGraph text file, MTF?.data, where ? stands for the screen
  7. number. The data file includes the dc and second harmonic.
  8. HISTORY:
  9. 10/11/90    dgp    wrote it.
  10. 10/12/90    dgp    added dc and harmonics.
  11. 3/18/91        dgp    updated to work with new PlotXY, but not tested
  12. 4/15/91        dgp Check for NewPaletteManager().
  13. 8/24/91        dgp    Made compatible with THINK C 5.0.
  14. 3/10/92    dgp    include mc68881.h
  15. 8/27/92    dgp    replace SysEnvirons() by Gestalt()
  16. 10/23/92 dgp try to read latest LuminanceRecord
  17. 2/7/93    dgp    updated to use SetPixelsQuickly.
  18. */
  19. #include "VideoToolbox.h"
  20. #include <math.h>
  21. #include <assert.h>
  22. #include "Luminance.h"
  23. #include <Packages.h>
  24. #if THINK_C
  25.     #include <profile.h>    /* for timing */
  26.     #include <console.h>
  27.     #define PROFILE 1
  28. #endif
  29. #define TWO_PI (2.0*PI)
  30. #define MAX(a,b) ((a)>(b)?(a):(b))
  31. #define MIN(a,b) ((a)<(b)?(a):(b))
  32.  
  33. void MeasureMTF(void);
  34. double MeasureContrast(GDHandle device,luminanceRecord *LP,int tPeriod,double c[10],WindowPtr plotWindow);
  35. double saw(double x);
  36.  
  37. void main(void)
  38. {
  39.     assert(StackSpace()>1000);
  40.     StackGrow(30000);
  41.     Require(gestalt8BitQD);
  42.     MeasureMTF();
  43. }
  44.  
  45. void MeasureMTF(void)
  46. {
  47.     GDHandle device,oldGDHandle;
  48.     CWindowPtr window;
  49.     WindowPtr plotWindow,mtfWindow;
  50.     luminanceRecord LR;
  51.     OSErr error;
  52.     char string[100],string2[100],outName[100];
  53.     unsigned long seconds;
  54.     FILE *outfile[2];
  55.     int i,tPeriod,width;
  56.     Rect r,srcRect,dstRect;
  57.     double a,b,f,fNominal,c,L,cOut,cH[10],cV[10];
  58.     PlotXYStyle mtfStyle[2];
  59.     unsigned long row[1048];    // hopefully long enough for any video device
  60.     short oldScreen;
  61.  
  62.     assert(StackSpace()>4000);
  63.     #if THINK_C
  64.         console_options.ncols=100;
  65.         MaximizeConsoleHeight();
  66.         printf("\n");                       /* Initialize QuickDraw */
  67.     #else
  68.         InitGraf(&qd.thePort);
  69.         InitFonts();
  70.         InitWindows();
  71.         InitCursor();
  72.     #endif
  73.     #if PROFILE
  74.         InitProfile(200,1);
  75.         _profile=0;    /* disable profiling for the moment */
  76.     #endif
  77.     printf("Welcome to MeasureMTF.\n");
  78.     #include "LuminanceRecord1.h"                    // read at compile time
  79.     oldScreen=LR.screen;
  80.     if(GetScreenDevice(1)!=NULL){
  81.         printf("Which screen would you like to calibrate (%d):",LR.screen);
  82.         gets(string);
  83.         sscanf(string,"%d",&LR.screen);
  84.     }
  85.     else LR.screen=0;
  86.     sprintf(string,"LuminanceRecord%d.h",LR.screen);
  87.     i=ReadLuminanceRecord(string,&LR,0);        // try to read latest luminanceRecord
  88.     if(i<1)printf("Warning: couldn't find “%s”. Calibrating screen %d.\n"
  89.         ,string,LR.screen);
  90.     else oldScreen=LR.screen;
  91.  
  92.     mtfStyle[0].continuing=0;
  93.     mtfStyle[0].lineWidth=1;
  94.     mtfStyle[0].symbolWidth=0;
  95.     mtfStyle[0].dash[0]=0;
  96.     mtfStyle[0].dashOffset=0;
  97.     mtfStyle[0].color=blackColor;
  98.     mtfStyle[1]=mtfStyle[0];
  99.     mtfStyle[1].color=blueColor;
  100.     
  101.     GetDateTime(&seconds);
  102.     sprintf(outName,"MTF%d.%s",LR.screen,DatedString(seconds));
  103.     outfile[0]=stdout;
  104.     outfile[1]=fopen(outName,"w");
  105.     if(outfile[1]==NULL) PrintfExit("Sorry, can't create \"%s\".\007\n",outName);
  106.     SetFileInfo(outName,'TEXT','QKPT');    /* for Kaleidagraph */
  107.  
  108.     /* Find device corresponding to the experimental screen. */
  109.     oldGDHandle=GetGDevice();
  110.     device = GetScreenDevice(LR.screen);
  111.     if(NewPaletteManager())
  112.         SetDepth(device,8,1,1);    /* 8-bit pixels, color mode */
  113.     window = GDOpenWindow(device);
  114.     
  115.     if(oldScreen==LR.screen){
  116.         printf("Luminance was calibrated on %s\n",LR.date);
  117.         printf("at %.2f %s by %s\n",LR.LBackground,LR.units,LR.notes);
  118.         printf("Range is %.1f to %.1f %s.\n",LR.LMin,LR.LMax,LR.units);
  119.     }
  120.     L=LR.LBackground;    /* desired mean luminance */
  121.     printf("Enter desired display luminance in cd/m^2 (%.2f):",L);
  122.     fgets(string,sizeof(string)-1,stdin);
  123.     sscanf(string,"%lf", &L);
  124.  
  125.     c=0.9;
  126.     c=MIN(c,MIN((LR.LMax-L)/L,(L-LR.LMin)/L));
  127.     printf("Contrast %.1f\n",c);
  128.     printf("Computing sinusoidal lookup table . . .\n");
  129.     for(i=0;i<256;i++){
  130.         SetLuminance(device,&LR,i,L*(1.0+c*sin(TWO_PI*i/256.0)),L*(1.0-c),L*(1.0+c));
  131.     }
  132.  
  133.     tPeriod=64;                                    /* frames in one temporal period */
  134.     width=window->portRect.right;                /* pixels across screen */
  135.     SetRect(&r,0,0,2*tPeriod,2*tPeriod);
  136.     OffsetRect(&r,64,64);
  137.     plotWindow=NewWindow(NULL,&r,"\pL",1,noGrowDocProc,(WindowPtr) -1L,FALSE,0L);
  138.     SetRect(&r,0,0,192,192);
  139.     OffsetRect(&r,640-r.right-64,64);
  140.     mtfWindow=NewWindow(NULL,&r,"\pMTF",1,noGrowDocProc,(WindowPtr) -1L,FALSE,0L);
  141.     MeasureContrast(device,&LR,tPeriod,cH,plotWindow);        /* measure vBlack */
  142.     ffprintf(outfile,
  143.         "notes" "\tcycles/pixel" "\tcycles/screen" 
  144.         "\tHoriz. Grating Gain" "\tVert. Grating Gain" "\tVert./Horiz.");
  145.     fprintf(outfile[0],"\n");
  146.     fprintf(outfile[1],"\tc" "\tcH[0]" "\tcH[1]" "\tcH[2]" "\tcV[0]" "\tcV[1]" "\tcV[2]" 
  147.         "\t(cV[0]-cH[0])/cH[0]" "\tcV[2]/(c*V/H)^2" "\t(cV[0]-cH[0])/cH[0]/(c*V/H)^2"
  148.         "\n");
  149.     ffprintf(outfile,"notes");    /* put some text in notes column */
  150.     for(fNominal=0.0;;fNominal=MAX(fNominal*pow(2.0,1.0/2.0),fNominal+1.0/width)){
  151.         f=floor(fNominal*width+0.5)/width; /* integral periods for zero mean over line */
  152.         if(fNominal>0.5)f=0.5;
  153.         /* horizontal grating */
  154.         srcRect=dstRect=window->portRect;
  155.         dstRect.left=srcRect.right=1;
  156.         for(i=0;i<srcRect.bottom;i++){
  157.             row[0]=128.0+127.5*saw(TWO_PI*f*i);
  158.             SetWindowPixelsQuickly((WindowPtr)window,0,i,row,1);
  159.         }
  160.         /* CopyBits uses the inverse color table of the current GDevice. */
  161.         SetGDevice(device);
  162.         CopyBits((BitMap *)*window->portPixMap,(BitMap *)*window->portPixMap,
  163.             &srcRect,&dstRect,srcCopy,NULL);
  164.         SetGDevice(oldGDHandle);
  165.         cOut=MeasureContrast(device,&LR,tPeriod,cH,plotWindow);
  166.         ffprintf(outfile,"\t%10.5f" "\t%10.2f" "\t%12.6f",f,f*dstRect.right,cOut/c);
  167.         /* draw the MTF */
  168.         PlotXY(mtfWindow
  169.             ,log(f/(1./640.))/log(0.5/(1./640.)), log(cOut/c/0.3)/log(1.1/0.3)
  170.             ,&mtfStyle[0]);
  171.  
  172.         /* vertical grating */
  173.         srcRect=dstRect=window->portRect;
  174.         dstRect.bottom=srcRect.top=srcRect.bottom-1;
  175.         for(i=0;i<srcRect.right;i++)row[i]=128.0+127.5*saw(TWO_PI*f*i);
  176.         SetWindowPixelsQuickly((WindowPtr)window,0,srcRect.top,row,srcRect.right);
  177.         SetGDevice(device);
  178.         CopyBits((BitMap *)*window->portPixMap,(BitMap *)*window->portPixMap,
  179.             &srcRect,&dstRect,srcCopy,NULL);
  180.         SetGDevice(oldGDHandle);
  181.         cOut=MeasureContrast(device,&LR,tPeriod,cV,plotWindow);
  182.         ffprintf(outfile,"\t%20.6f" "\t%18.6f",cOut/c,cV[1]/cH[1]);
  183.         fprintf(outfile[0],"\n");
  184.         fprintf(outfile[1],"\t%f" "\t%f" "\t%f" "\t%f" "\t%f" "\t%f" "\t%f"
  185.             ,c,cH[0],cH[1],cH[2],cV[0],cV[1],cV[2]);
  186.         a=(cV[0]-cH[0])/cH[0];    /* dc error, expressed as a contrast */
  187.         b=cV[1]/cH[1];            /* contrast gain of video amplifier */
  188.         fprintf(outfile[1],"\t%f" "\t%f" "\t%f" "\n"
  189.             ,a,cV[2]/(c*b*c*b),a/(c*b*c*b));
  190.         /* draw the MTF */
  191.         PlotXY(mtfWindow
  192.             ,log(f/(1./640.))/log(0.5/(1./640.)), log(cOut/c/0.3)/log(1.1/0.3)
  193.             ,&mtfStyle[1]);
  194.         
  195.         if(f==0.5)break;
  196.     }
  197.     
  198.     /* print notes */
  199.     IUDateString(seconds,longDate,(unsigned char *)string);
  200.     p2cstr((unsigned char *)string);
  201.     IUTimeString(seconds,FALSE,(unsigned char *)string2);
  202.     p2cstr((unsigned char *)string2);
  203.     ffprintf(outfile,"%s %s\n",string2,string);
  204.     ffprintf(outfile,"%.1f Hz\n",1.0/(tPeriod*0.015));
  205.     ffprintf(outfile,"%.2f cd/m^2\n",L);
  206.     ffprintf(outfile,"%.3f contrast\n",c);
  207.     ffprintf(outfile,"\n" "screen %d\n",LR.screen);
  208.     ffprintf(outfile,"\"%s\" monitor %s\n",LR.name,LR.id);
  209.     ffprintf(outfile,"on %s\n",LR.date);
  210.     ffprintf(outfile,"by %s\n",LR.notes);
  211.     ffprintf(outfile,"at %.2f %s\n",LR.LBackground,LR.units);
  212.  
  213.     GDDisposeWindow(device,window);
  214.     if(outfile[1] != NULL)fclose(outfile[1]);
  215.     printf("The sum of the times for GetVoltage() and LoadLuminances() should\n"
  216.         "be about one frame, 15 ms. If it's longer, e.g. 30 ms, you should reduce\n"
  217.         "the value of \"frames\" in MeasureContrast().\n");
  218.     DisposeWindow(plotWindow);
  219.     DisposeWindow(mtfWindow);
  220. }
  221.  
  222.  
  223. double MeasureContrast(GDHandle device,luminanceRecord *LP
  224.     ,int tPeriod,double c[10],WindowPtr plotWindow)
  225. /*
  226. Measures the contrast of a drifting grating of unknown phase.
  227. The temporal period is tPeriod frames. The c[] array is
  228. filled with the amplitude spectrum, where c[i] is the contrast of the i-th harmonic,
  229. except that c[0], which would always be 1, instead is set to the dc level.
  230. */
  231. {
  232.     double v[256];
  233.     double p[10];
  234.     register int i,j;
  235.     ColorSpec doubleTable[512];    // was static, dgp 6/15/94
  236.     static double sinTable[256],cosTable[256];
  237.     static int tablePeriod=0;
  238.     register double a,b;
  239.     double frequency=2000.,gain=100.;
  240.     double frames=0.6;    /* How long the A/D should spend sampling the photometer */
  241.     long n;
  242.     static double vBlack=0.0;
  243.     static int blackSet=0;
  244.     char string[80];
  245.     WindowPtr oldPort;
  246.     int v0,ip;
  247.  
  248.     assert(StackSpace()>4000);
  249.     if(tPeriod>256){
  250.         printf("Warning. Reducing tPeriod to 256.\n");
  251.         tPeriod=256;
  252.     }
  253.     if(tablePeriod != tPeriod){
  254.         for(i=0;i<tPeriod;i++){
  255.             sinTable[i]=sin(i*TWO_PI/tPeriod);
  256.             cosTable[i]=cos(i*TWO_PI/tPeriod);
  257.         }
  258.         tablePeriod=tPeriod;
  259.     }
  260.     
  261.     n=(long)floor(0.5+frequency*0.015*frames);
  262.  
  263.     RemeasureContrast:
  264.     if(!blackSet){
  265.         doubleTable[0].rgb.red=doubleTable[0].rgb.green=doubleTable[0].rgb.blue=0;
  266.         for(i=0;i<256;i++)doubleTable[i]=doubleTable[i+256]=doubleTable[0];
  267.         LoadLuminances(device
  268.             ,(luminanceRecord *)(doubleTable+(i%tPeriod)*(256/tPeriod)),0,255);
  269.         printf("Please block all light to set black. Hit cr when ready:");
  270.         gets(string);
  271.         vBlack=0.0;
  272.     }
  273.     else for(i=0;i<256;i++)doubleTable[i]=doubleTable[i+256]=LP->table[i];
  274.  
  275.     /* warm up for one period before collecting data */
  276.     SetPriority(7);
  277.     for(i=0;i<tPeriod;i++){
  278.         LoadLuminances(device
  279.             ,(luminanceRecord *)(doubleTable+(i%tPeriod)*(256/tPeriod)),0,255);
  280.         GetVoltage(1,&gain,&frequency,n,NULL);
  281.         v[i]=0.0;
  282.     }
  283.     _profile=1;
  284.     for(i=0;i<tPeriod*4;i++){
  285.         LoadLuminances(device
  286.             ,(luminanceRecord *)(doubleTable+(i%tPeriod)*(256/tPeriod)),0,255);
  287.         v[i%tPeriod]+=GetVoltage(1,&gain,&frequency,n,NULL);
  288.     }
  289.     _profile=0;
  290.     SetPriority(0);
  291.     a=0.0;
  292.     for(i=0;i<tPeriod;i++) a+=v[i];
  293.     c[0]=a/tPeriod-vBlack;
  294.     if(!blackSet){
  295.         vBlack=c[0];
  296.         blackSet=1;
  297.         printf("Black %g mV\n",vBlack*1000.0);
  298.         printf("Now please remove light block. Hit cr when ready:");
  299.         gets(string);
  300.         goto RemeasureContrast;
  301.     }
  302.     for(j=1;j<10;j++){
  303.         a=b=0.0;
  304.         for(i=0;i<tPeriod;i++){
  305.             a+=sinTable[i*j%tPeriod]*v[i];
  306.             b+=cosTable[i*j%tPeriod]*v[i];
  307.         }
  308.         c[j]=2.0*sqrt(a*a+b*b)/tPeriod/c[0];
  309.         p[j]=atan2(a,b);
  310.     }
  311.     /* Show one period of raw data, fundamental, raw minus fundamental, 2nd harmonic */
  312.     GetPort(&oldPort);
  313.     SetPort(plotWindow);
  314.     BringToFront(plotWindow);
  315.     EraseRect(&plotWindow->portRect);
  316.     v0=plotWindow->portRect.bottom/4;
  317.     SetOrigin(0,-2*v0);
  318.     MoveTo(0,-v0*(v[0]-vBlack)/c[0]);
  319.     for(i=1;i<tPeriod;i++)LineTo(i,-v0*(v[i]-vBlack)/c[0]);
  320.     SetOrigin(0,-4*v0);
  321.     j=1;
  322.     ip=tPeriod*(1.0-p[j]/TWO_PI);
  323.     a=-v0*(0.0+c[j]*cosTable[(i*j+ip)%tPeriod]);
  324.     MoveTo(0,-v0*(v[0]-vBlack)/c[0]-a);
  325.     for(i=1;i<tPeriod;i++){
  326.         a=-v0*(0.0+c[j]*cosTable[(i*j+ip)%tPeriod]);
  327.         LineTo(i,-v0*(v[i]-vBlack)/c[0]-a);
  328.     }
  329.     ForeColor(blueColor);
  330.     for(j=1;j<3;j++){
  331.         SetOrigin(-tPeriod,-2*v0*j);
  332.         ip=tPeriod*(1.0-p[j]/TWO_PI);
  333.         i=0;
  334.         a=-v0*(1.0+c[j]*cosTable[(i*j+ip)%tPeriod]);
  335.         MoveTo(0,a);
  336.         for(i=1;i<tPeriod;i++){
  337.             a=-v0*(1.0+c[j]*cosTable[(i*j+ip)%tPeriod]);
  338.             LineTo(i,a);
  339.         }
  340.     }
  341.     ForeColor(blackColor);
  342.     SetOrigin(0,0);
  343.     SetPort(oldPort);
  344.     return c[1];
  345. }
  346.  
  347.  
  348. double saw(double x)
  349. /* returns sawtooth function with same phase, amplitude, and symmetry as sin() */
  350. {
  351.     x/=TWO_PI;
  352.     x-=0.5;
  353.     x-=floor(x);
  354.     return 2.0*x-1.0;
  355. }
  356.